let interval;
let intime = 50
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    },
    luckPosition: {
      type: Number
    }
  },
  observers: {
    luckPosition: function (val) {
      if (val * 1 !== -1) {
        this.lottery()
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    status: -1,
    lucky: {}
  },
  ready() {
    // this.loadAnimation();
  },
  detached() {
    clearInterval(interval)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //点击抽奖按钮
    startLucks() {
      this.triggerEvent('start')
    },
    lottery() {
      let {
        luckPosition,
        list,
        isDisabled
      } = this.data
      if (isDisabled) {
        return
      }
      if (!isDisabled) {
        //判断中奖位置格式
        if (luckPosition == null || isNaN(luckPosition) || luckPosition > 7 || list.length === 0) {
          wx.showModal({
            title: '提示',
            content: '系统发生错误，请稍后重试',
            showCancel: false
          });
          return;
        }
        //设置按钮不可点击
        this.setData({
          isDisabled: true
        })
        //清空计时器
        clearInterval(interval);
        let index = 0;
        //循环设置每一项的透明度
        interval = setInterval(() => {
          if (index > 7) {
            index = 0;
            list[7].opa = 0.9
          } else if (index != 0) {
            list[index - 1].opa = 0.9
          }
          list[index].opa = 1;
          index++;
          this.setData({
            list
          })
        }, intime);
        //模拟网络请求时间  设为两秒
        var stoptime = 2000;
        setTimeout(() => {
          this.stop(luckPosition);
        }, stoptime)
      }
    },
    stop(which) {
      let {
        list
      } = this.data

      //清空计数器
      clearInterval(interval);
      //初始化当前位置
      let current = -1;

      for (let i = 0; i < list.length; i++) {
        if (list[i].opa == 1) {
          current = i;
        }
      }
      //下标从1开始
      let index = current + 1;
      this.stopLuck(which, index, intime, 10);
    },
    stopLuck(which, index, time, splittime) {
      let { list } = this.data
      //值越大出现中奖结果后减速时间越长
      setTimeout(() => {
        //重置前一个位置
        if (index > 7) {
          index = 0;
          list[7].opa = 0.9
        } else if (index != 0) {
          list[index - 1].opa = 0.9
        }
        //当前位置为选中状态
        list[index].opa = 1;
        this.setData({
          list
        })
        //如果旋转时间过短或者当前位置不等于中奖位置则递归执行
        //直到旋转至中奖位置
        if (time < 400 || index != which) {
          //越来越慢
          splittime++;
          time += splittime;
          //当前位置+1
          index++;
          this.stopLuck(which, index, time, splittime);
        } else {
          //中奖
          const { list } = this.data
          const lucky = list[which]
          if (lucky.prize_name !== '谢谢参与') {
            this.setData({
              status: 1,
              lucky
            })
          } else {
            this.setData({
              status: 0
            })
          }
        }
      }, time);
    },
    loadAnimation() {
      let index = 0;
      let {
        list
      } = this.data
      clearInterval(interval)
      interval = setInterval(() => {
        if (index > 7) {
          index = 0;
          list[7].opa = 0.7
        } else if (index != 0) {
          list[index - 1].opa = 0.7
        }
        list[index].opa = 1;
        index++;
        this.setData({
          list
        })
      }, 1000);
    },
    viewRule() {
      this.triggerEvent('rule')
    },
    closeWin() {
      this.setData({
        status: -1
      })
     this.triggerEvent('order')
    },
    goShopping() {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  }
})
